﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using UBCon.RibbonDemo.Models;
using UBCon.Core.Commands;
using System.Diagnostics;
using UBCon.Resources;
using UBCon.Core.Enums;

namespace UBCon.RibbonDemo.ViewModels
{
    /// <summary>
    /// 
    /// </summary>
    public class ShellViewModel:INotifyPropertyChanged
    {

        #region Constructors

        public ShellViewModel() 
        {
            InitializeItemsSources();
          //  RegisterCommands();
        }

        #endregion

        #region Properties

        public RibbonBarMode RibbonBarMode 
        {
            get 
            {
                return _ribbonBarMode;
            }
            set 
            {
                if (_ribbonBarMode != value) 
                {
                    OptionsItemModel model = (from mode in OptionsButtonSource where mode.Mode == value select mode).First();
                    if (model != SelectedOptionItem) 
                    {
                        SelectedOptionItem = model;
                    }
                    _ribbonBarMode = value;
                    OnPropertyChanged("RibbonBarMode");
                }
                
            }
        }

        public ObservableCollection<OptionsItemModel> OptionsButtonSource 
        {
            get 
            {
                if (optionsButtonSource == null) 
                {
                    optionsButtonSource = new ObservableCollection<OptionsItemModel>();
                }
                return optionsButtonSource;
            }
            set 
            {
                optionsButtonSource = value;
            }
        }

        public int MaxVisibleItemsCount 
        {
            get 
            {
                return maxVisibleItemsCount;
            }
            set 
            {
                maxVisibleItemsCount = value;
                OnPropertyChanged("MaxVisibleItemsCount");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Command OnPasteCommand 
        {
            get 
            {
                return onPasteCommand;
            }
            set 
            {
                onPasteCommand = value;
            }
        }
        /// <summary>
        /// Gets or sets items for RibbonGallery.
        /// </summary>
        public ObservableCollection<ContentModel> GalleryItemsSource 
        {
            get 
            {
                return galleryItemsSource;
            }
            set 
            {
                galleryItemsSource = value;
                OnPropertyChanged("GalleryItemsSource");
            }
        }

        public ObservableCollection<ContentModel> GalleryBullets 
        {
            get 
            {
                return galleryBullets;
            }
            set 
            {
                galleryBullets = value;
                OnPropertyChanged("GalleryBullets");
            }
        }

        public ObservableCollection<ContentModel> GalleryNumbers 
        {
            get { return galleryNumbers; }
            set 
            {
                galleryNumbers = value;
                OnPropertyChanged("GalleryNumbers");
            }
        }

        public ObservableCollection<ContentModel> UnderlineItems 
        {
            get 
            {
                return underlineItems;
            }
            set 
            {
                underlineItems = value;
                OnPropertyChanged("UnderlineItems");
            }
        }

        public OptionsItemModel SelectedOptionItem 
        {
            get 
            {
                return _selectedOptionItem;
            }
            set 
            {
                //bool _shouldRaiseEvent = !ReferenceEquals(_selectedOptionItem, value);
                if (!ReferenceEquals(_selectedOptionItem, value))
                {
                    _selectedOptionItem = value;
                    OnPropertyChanged("SelectedOptionItem");
                }

                //if (_shouldRaiseEvent) 
                //{
                //    OnPropertyChanged("SelectedOptionItem");
                //}
                if (_selectedOptionItem != null) 
                {
                    if (RibbonBarMode != _selectedOptionItem.Mode) 
                    {
                        RibbonBarMode = _selectedOptionItem.Mode;
                    }
                }
            }
        }
        #endregion

        #region Methods

        private void RegisterCommands() 
        {
            OnPasteCommand = new Command(OnPasteCommandExecuted);
        }

        private void OnPasteCommandExecuted() 
        {
            GalleryItemsSource = null;
            GalleryItemsSource = new ObservableCollection<ContentModel>();
        }

        private void InitializeItemsSources() 
        {
            GalleryItemsSource = new ObservableCollection<ContentModel>();

            for (int i = 0; i < 11; i++)
            {
                if (i >= 1)
                {
                    GalleryItemsSource.Add(
                        new ContentModel() 
                        { 
                            Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Paragraph/paragraph" + i.ToString() + ".png", 
                            ImageHeight = 48, 
                            ImageWidth = 63 
                        });
                }
                else
                {
                    GalleryItemsSource.Add(
                        new ContentModel() 
                        { 
                            Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Paragraph/paragraph" + ".png", 
                            ImageHeight = 48, 
                            ImageWidth = 63 
                        });
                }
            }

            GalleryBullets = new ObservableCollection<ContentModel>();

            for (int i = 0; i < 8; i++)
            {
                if (i >= 1)
                {
                    GalleryBullets.Add(
                        new ContentModel() 
                        { 
                            Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Gallery/GalleryBulletsType" + i.ToString() + ".png", 
                            ImageHeight=32, 
                            ImageWidth=32 
                        });
                }
                else 
                {
                    GalleryBullets.Add(
                        new ContentModel() 
                        { 
                            Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Gallery/GalleryBulletsNone.png", 
                            ImageHeight = 32, 
                            ImageWidth = 32 
                        });
                
                }
            }

            GalleryNumbers = new ObservableCollection<ContentModel>();
            for (int i = 0; i < 8; i++)
            {
                if (i >= 1)
                {
                    GalleryNumbers.Add(
                        new ContentModel() 
                        { 
                            Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Gallery/GalleryNumbersType" + i.ToString() + ".png", 
                            ImageHeight = 70, 
                            ImageWidth = 70 
                        });
                }
                else
                {
                    GalleryNumbers.Add(
                        new ContentModel() 
                        { 
                            Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Gallery/GalleryNumbersNone.png", 
                            ImageHeight = 70, 
                            ImageWidth = 70 
                        });
                }
            }

            UnderlineItems = new ObservableCollection<ContentModel>();

            for (int i = 1; i <= 8; i++)
            {
                UnderlineItems.Add(
                    new ContentModel() 
                    { 
                        Image = "pack://application:,,,/UBCon.RibbonDemo;component/images/FirstLook/Underline/underline" + i.ToString() + ".png", 
                        ImageHeight = 15, 
                        ImageWidth=160 
                    });
            }

            InitializeOptionsButtonSource();
        }

        private void InitializeOptionsButtonSource()
        {
            OptionsItemModel model = new OptionsItemModel();
            model.Mode = RibbonBarMode.AutoHideMode;
            
            model.Title = LocalizationResources.AutomaticallyHideRibbonBarText;
            model.Description = LocalizationResources.AutomaticallyHideRibbonBarDescriptionText;
            model.ImageSource = LocalizationResources.AutomaticallyHideRibbonBarImage;
            if (RibbonBarMode == model.Mode)
            {
                SelectedOptionItem = model;
            }
            OptionsButtonSource.Add(model);

            model = new OptionsItemModel();
            model.Mode = RibbonBarMode.ShowOnlyTabs;
            model.Title = LocalizationResources.ShowTabItemsText;
            model.Description = LocalizationResources.ShowTabItemsTextDescriptionText;
            model.ImageSource = LocalizationResources.ShowTabItemsImage;
            if (RibbonBarMode == model.Mode)
            {
                SelectedOptionItem = model;
            }
            OptionsButtonSource.Add(model);

            model = new OptionsItemModel();
            model.Mode = RibbonBarMode.ShowTabsAndCommands;
            model.Title = LocalizationResources.ShowTabItemsAndCommandText;
            model.Description = LocalizationResources.ShowTabItemsAndCommandDescriptionText;
            model.ImageSource = LocalizationResources.ShowTabItemsAndCommandImage;
            if (RibbonBarMode == model.Mode)
            {
                SelectedOptionItem = model;
            }
            OptionsButtonSource.Add(model);

        }

        private void OnPropertyChanged(string propertyName) 
        {
            if (PropertyChanged != null) 
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private const string IsSelectedPropertyName = "IsSelected";
        #endregion

        #region INotifyPropertyChanged Implementation
        
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region PrivateFields

        private OptionsItemModel _selectedOptionItem = null;

        ObservableCollection<OptionsItemModel> optionsButtonSource;

        private ObservableCollection<ContentModel> galleryItemsSource, galleryBullets, galleryNumbers, underlineItems;

        string[] fileNames = new string[188] 
        {
            "adduser.png",
            "addusers.png",
            "Add_1.png",
            "AlignCenter_16x16.png",
            "AlignLeft_16x16.png",
            "AlignRight_16x16.png",
            "applicationexit.png",
            "badmail.png",
            "Bold_16x16.png",
            "book.ico",
            "Book.png",
            "Book_angleHS.png",
            "BordersAndShading_16x16.png",
            "Borders_16x16.png",
            "BottomBorder_16x16.png",
            "Bullets_16x16.png",
            "b_calendar.png",
            "Calendar-Next.png",
            "CheckGrammarHS.png",
            "CheckSpellingHS.png",
            "ChooseColor_16x16.png",
            "clean.png",
            "ClearFormatting_16x16.png",
            "Close_16x16.png",
            "ColorPalette.png",
            "Color_16x16.png",
            "Computer16.png",
            "connected_multiple.jpg",
            "ConnectionManager_48x48.png",
            "copy.png",
            "Copy16.png",
            "Copy_16x16.png",
            "CreateFolder16.png",
            "cut.png",
            "cut16.png",
            "cut32.png",
            "Cut_16x16.png",
            "danger32.png",
            "DecreaseIndent_16x16.png",
            "Default_16x16.png",
            "delete16.png",
            "delete32.png",
            "deleteuser.png",
            "DiagonalDownBorder_16x16.png",
            "DiagonalUpBorder_16x16.png",
            "Document_16x16.png",
            "DownArrow_16x16.png",
            "DownArrow_32x32.png",
            "DrawTable_16x16.png",
            "EditRedo_16x16.png",
            "EditUndo_16x16.png",
            "Erase_16x16.png",
            "favorites16.png",
            "favorites32.png",
            "Find_16x16.png",
            "Find_32x32.png",
            "Folder-Rename.png",
            "folder.png",
            "folder16.png",
            "folder32.png",
            "FolderClosed.png",
            "FolderClosed_48x48.png",
            "foldergreen.png",
            "FolderLogo.png",
            "FolderOpen.png",
            "FolderSelected.png",
            "FontColor_16x16.png",
            "FontScript_16x16.png",
            "Font_16x16.png",
            "Font_32x32.png",
            "Forecolor_16x16.png",
            "FormatPainter_16x16.png",
            "goodmail.png",
            "GoTo_16x16.png",
            "GWE.ICO",
            "Help_16x16.png",
            "Highlight_16x16.png",
            "history16.png",
            "history32.png",
            "Home.png",
            "home16.png",
            "home32.png",
            "Import.png",
            "IncreaseIndent_16x16.png",
            "Info.png",
            "InsideBorders_16x16.png",
            "InsideHorizontalBorder_16x16.png",
            "InsideVerticalBorder_16x16.png",
            "Italic_16x16.png",
            "Journal.png",
            "Justify_16x16.png",
            "LeftArrow_32x32.png",
            "LeftBorder_16x16.png",
            "LineColor_16x16.png",
            "LineSpacing_16x16.png",
            "mail.png",
            "mail16.png",
            "mail32.png",
            "MailFile16.png",
            "mail_new.png",
            "Minus_32x32.png",
            "Move16.png",
            "Movie.png",
            "msg_good.png",
            "msg_question.png",
            "MultilevelList_16x16.png",
            "Music.png",
            "MusicOnLine16.png",
            "MyComputer16.png",
            "MyDocuments16.png",
            "MyMusic32.png",
            "MyNetwork16.png",
            "MyPictures16.png",
            "MyPictures32.png",
            "MyPictures48.png",
            "NetworkFavorites16.png",
            "NEW.BMP",
            "NewDocument_16x16.png",
            "NewPermission_32x32.png",
            "Next.png",
            "NoBorder_16x16.png",
            "Notebook-Save.png",
            "Numbering_16x16.png",
            "Open_16x16.png",
            "Options_16x16.png",
            "OrderPictures16.png",
            "OuterBorders_16x16.png",
            "packet_sniffer.png",
            "ParagraphSpacing_16x16.png",
            "Paragraph_16x16.png",
            "Paragraph_32x32.png",
            "paste16.png",
            "paste32.png",
            "Paste_16x16.png",
            "Paste_32x32.png",
            "PlayAll16.png",
            "Plus_32x32.png",
            "Previous.png",
            "Print-Quick.png",
            "Printer_48x48.png",
            "PrintPictures16.png",
            "PrintPreview_16x16.png",
            "Print_16x16.png",
            "props16.png",
            "props32.png",
            "PublishFolder16.png",
            "PublishPlan_16x16.png",
            "README.html",
            "RecycleBin.png",
            "RefreshArrow_32x32.png",
            "Remove.png",
            "Rename16.png",
            "Replace_16x16.png",
            "RightArrow_32x32.png",
            "RightBorder_16x16.png",
            "save16.png",
            "save32.png",
            "Save_16x16.png",
            "Search.png",
            "search16.png",
            "search32.png",
            "SelectionPane_16x16.png",
            "Select_16x16.png",
            "SendLinkByEmail_32x32.png",
            "Shading_16x16.png",
            "SharedMusic16.png",
            "ShareFolder16.png",
            "ShowGridlines_16x16.png",
            "ShowHide_16x16.png",
            "SlideShow16.png",
            "Sort_16x16.png",
            "Star.png",
            "StylesGroup.png",
            "Styles_32x32.png",
            "Table_32x32.png",
            "ThemeFonts.png",
            "Thumbs.db",
            "Tick_32x32.png",
            "TopBorder_16x16.png",
            "undo16.png",
            "undo32.png",
            "UpArrow_16x16.png",
            "UpArrow_32x32.png",
            "Users.png",
            "Window-Refresh.png",
            "WinFolder.gif",
            "WORD.ico",
            "word2010.png"
  
        };

        private Command onPasteCommand;

        int maxVisibleItemsCount = 5;
        private RibbonBarMode _ribbonBarMode = RibbonBarMode.ShowTabsAndCommands;
        #endregion
    }
}
